﻿#include "EditorAbilityGroupView.h"
#include "Framework/Editor.h"
#include "Ability/EditorAbilityViewModel.h"
#include "EditorAbilityGroupViewModel.h"
#include "Widgets/Positioners/FunctionPositionerWidget.h"
#include "Widgets/ImageWidget.h"
#include "Widgets/TextWidget.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Widgets/RectangleWidget.h"
#include "Object/NewObject.h"

#include "Widgets/EditorImageButton.h"
#include "Widgets/Splitable/EditorSplitableLayoutView.h"

void REditorAbilityGroupView::BindViewModel(TSharedObjectPtr<REditorAbilityViewModel> InViewModel)
{
    Super::BindViewModel(InViewModel);
    GroupViewModel = InViewModel;

    Layout=NewObject<REditorSplitableLayoutView>(this);
    Layout->OnBindItemView.BindLambda(
        [this](RObjectPtr InViewModel) //返回视图
        {
            TSharedObjectPtr<REditorAbilityViewModel> AbilityViewModel=InViewModel;
            TSharedObjectPtr<REditorAbilityView> AbilityView = NewObject<REditorAbilityView>(
                REditor::Get()->GetViewClass(AbilityViewModel->GetClass())
                ,this);

            AbilityView->BindViewModel(AbilityViewModel);
            return AbilityView;

        }
    );
    Layout->OnFillItemWidget.Bind(this,&REditorAbilityGroupView::FillAbilityWidget);

    Layout->BindViewModel(GroupViewModel->GetLayout());

}

void REditorAbilityGroupView::CreateWidget(TSharedObjectPtr<RWidget> InParent)
{
    auto Root=NewWidget<RFunctionPositionerWidget>(InParent);
    Root->Anchor.Fill(InParent);

    Layout->CreateWidget(Root);
}

void REditorAbilityGroupView::FillOperations(TSharedObjectPtr<RWidget> InTitleBar)
{
    auto MoreButton=WHE::CreateImageButton(
        GroupViewModel->GetMoreIcon()
        ,CString(U"更多操作")
        ,[](){
           //TODO: show more menu
        }
        ,InTitleBar
    );
    MoreButton->Anchor.RightReference=InTitleBar;
    MoreButton->Anchor.VerticalCenterReference=InTitleBar;
}

void REditorAbilityGroupView::FillAbilityWidget(RObjectPtr InAbilityView, TSharedObjectPtr<RWidget> InParentWidget)
{
    TWeakObjectPtr<REditorAbilityView> WeakAbilityView=InAbilityView;

    auto AbilityRoot=NewWidget<RWidget>(InParentWidget);
    AbilityRoot->Anchor.Fill(InParentWidget);
    //创建标题
    auto TitleWidget=NewWidget<RWidget>(AbilityRoot);
    TitleWidget->Anchor.HorizontalFill(AbilityRoot);
    TitleWidget->Height=GroupViewModel->GetAbilityTitleHeight();
        
    auto TitleExpandIcon=NewWidget<RImageWidget>(TitleWidget);
    TitleExpandIcon->Width=GroupViewModel->ExpandIconSize;
    TitleExpandIcon->Height=GroupViewModel->ExpandIconSize;
    TitleExpandIcon->Anchor.VerticalCenterReference=TitleWidget;
    TitleExpandIcon->Image=[this,WeakAbilityView](){
        return GroupViewModel->GetExpandIcon(
            WeakAbilityView->GetBaseViewModel()->IsExpanded()
        );
    };
    
    auto TitleText=NewWidget<RTextWidget>(TitleWidget);
    TitleText->X=GroupViewModel->ExpandIconSize;
    TitleText->Text=
    [this,WeakAbilityView]()->CString
    {
        return WeakAbilityView->GetBaseViewModel()->GetName();
    };
    // TitleText->Visible=[this](){
    //     return GroupViewModel->IsVertical();
    // };

    auto TitleMouse=NewWidget<RMouseWidget>(TitleWidget);
    TitleMouse->Anchor.Fill(TitleWidget);
    TitleMouse->OnClicked.BindLambda([this,WeakAbilityView](const SMouseButtonEvent&){
        WeakAbilityView->GetBaseViewModel()->ToggleIsExpanded();
        return true;
    });
    RMouseWidgetWeakPtr TitleMouseWeak=TitleMouse;

    auto TitleOverlay=NewWidget<RWidget>(TitleWidget);
    TitleOverlay->Anchor.Fill(TitleWidget);
    TitleOverlay->Visible=[TitleMouseWeak](){
        return TitleMouseWeak->IsHovered();
    };
    WeakAbilityView->FillOperations(TitleOverlay);

    //填充功能内容
    auto AbilityContentWidget=NewWidget<RWidget>(AbilityRoot);
    AbilityContentWidget->Anchor.HorizontalFill(AbilityRoot);
    AbilityContentWidget->Anchor.TopReference=TitleWidget;
    AbilityContentWidget->Anchor.TopReferenceEdge=EVerticalWidgetEdge::Bottom;
    AbilityContentWidget->Anchor.BottomReference=AbilityRoot;
    AbilityContentWidget->bClipSubWidgets=true;

    WeakAbilityView->CreateWidget(AbilityContentWidget);    
}
